package com.woniu.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class ConnectionUtils {

	private static Connection conn;
	private static String driverClass;
	private static String jdbcUrl;
	private static String username;
	private static String password;
	
	static {
		Properties pro = new Properties();
		try {
			pro.load(ConnectionUtils.class.getClassLoader().getResourceAsStream("db_config.properties"));
			driverClass = pro.getProperty("driverClass");
			jdbcUrl = pro.getProperty("jdbcUrl");
			username = pro.getProperty("username");
			password = pro.getProperty("password");
		} catch (IOException e) {
			System.out.println("读取配置文件失败,错误原因:"+e.getMessage());
		}
	}
	
	public static Connection getConnection() {
		if (conn == null) {
			try {
				Class.forName(driverClass);
				conn = DriverManager.getConnection(jdbcUrl, username, password);
			} catch (ClassNotFoundException e) {
				System.out.println("加载数据库驱动失败,失败原因:" + e.getMessage());
			} catch (SQLException e) {
				System.out.println("打开数据库失败:");
			}
		}
		return conn;
	}
	
	
}
